/* 
 * File:   main.cpp
 * Author: Muqtafi Akhmad
 *
 * Created on November 2, 2012, 3:53 PM
 */

#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include "../CalendarManager/CalendarManager.h"
#include "../CommandManager/CommandManager.h"
#include "../CalendarRenderer/CalendarRenderer.h"

using namespace std;

void SplitCommand(string text, char splitChar, char** result) {
    stringstream stream(text);
    int i;
    string word;
    vector<string> splitresult;
    while (getline(stream, word, splitChar)) {
        splitresult.push_back(word);
    }
    for (int i = 0; i < splitresult.size(); ++i) {
        result[i] = (char*) splitresult[i].c_str();
    }
}

/*
 * 
 */
int main(int argc, char** argv) {
    CalendarManager cal_manager;
    CommandManager com_manager(cal_manager);
    char** args = (char**) malloc(100 * sizeof (char*));
    bool programRunning = true;

    Command load("Load", &Calendar::LoadCalendar);
    com_manager.AddCommand(load);
    Command save("Save", &Calendar::SaveCalendar);
    com_manager.AddCommand(save);
    Command view("View", &Calendar::ViewEvent);
    com_manager.AddCommand(view);
    Command last("Last", &Calendar::PrintLast);
    com_manager.AddCommand(last);
    Command first("First", &Calendar::PrintFirst);
    com_manager.AddCommand(first);
    Command add("Add", &Calendar::AddEvent);
    com_manager.AddCommand(add);
    Command delete_("Delete", &Calendar::DeleteEvent);
    com_manager.AddCommand(delete_);
    Command show_detail("ShowDetail", &Calendar::ShowDetail);
    com_manager.AddCommand(show_detail);

    do {
        string commandBuff;
        printf("\n>");
        cin >> commandBuff;
        SplitCommand(commandBuff, ' ', args);
        if (!strcmp(args[0], "ShowCalendar")) {
            printf("(!)Execute command\n");
            int year = atoi(args[2]), month = atoi(args[3]);
            CalendarRenderer renderer(&(com_manager.GetCalendarManager().GetCalendar(atol(args[1]))), month, year);
            renderer.ShowCalendar(args);
            printf("(!)Execute success\n");
        } else if (!strcmp(args[0], "Erase")) {
            printf("(!)Execute command\n");
            com_manager.GetCalendarManager().DeleteCalendar(atol(args[1]));
            printf("(!)Execute success\n");
        } else if (!strcmp(args[0], "Create")) {
            printf("(!)Execute command\n");
            Calendar added_cal;
            added_cal.SetEntryNo(atol(args[1]));
            com_manager.GetCalendarManager().AddCalendar(added_cal);
            printf("(!)Execute success\n");
        } else if (strcmp(args[0], "Quit") && com_manager.IsCommandExist(args[0])) {
            printf("(!)Execute command\n");
            com_manager.Execute(atol(args[1]), args[0], args);
            printf("(!)Execute success\n");
        } else if (!strcmp(args[0], "Quit")) {
            printf("(!)Quit program\n");
            programRunning = false;
        } else {
            printf("(!)Unknown command %s \n", args[0]);
            printf("*** CSCalendar Help ***\n");
            printf("Available commands : \n");
            printf("1. Create <#cal_id> \n");
            printf("2. Erase <#cal_id> \n");
            printf("3. Load <#cal_id> <filename> \n");
            printf("4. View <#cal_id> <#event> \n");
            printf("5. Last <#cal_id> <number of event> \n");
            printf("6. First <#cal_id> <number of event> \n");
            printf("7. ShowCalendar <#cal_id> <YYYY> <MM> \n");
            printf("8. ShowDetail <#cal_id> <YYYY-MM-DD> \n");
            printf("9. Add <#cal_id> <#event>\n");
            printf("10. Delete <#cal_id> <#event>\n");
            printf("11. Save <#cal_id> <filename>\n");
            printf("12. Quit \n");
        }
    } while (programRunning);

    return 0;
}

